package com.cloudxingkong.dianyinke.NewTeacher.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.util.LruCache;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.bean.TeacherVideos;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.NewTeacher.bean.UVTest;
import com.cloudxingkong.dianyinke.Student.Utils.MySwipeView;
import com.cloudxingkong.dianyinke.TeacherApp;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 云星空科技 on 2016/3/18.
 */
public class UVAdapter extends BaseAdapter {
    private List<TeacherVideos.ListBean> uvData;
    private Context context;
    private ListView listView;
    private onTouchVideoDel onTouchVideoDel;
    public UVAdapter(List<TeacherVideos.ListBean> uvData, Context context,ListView listView,onTouchVideoDel onTouchVideoDel) {
        this.onTouchVideoDel=onTouchVideoDel;
        this.listView=listView;
        this.uvData = uvData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return uvData.size();
    }

    @Override
    public Object getItem(int position) {
        return uvData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView=View.inflate(context, R.layout.item_uploadvideo,null);
            holder=new ViewHolder();
            holder.avator= (CircleImageView) convertView.findViewById(R.id.uv_iv_avator);
            holder.commend= (TextView) convertView.findViewById(R.id.uv_tv_commend);
            holder.playTimes= (TextView) convertView.findViewById(R.id.uv_tv_bofangtimes);
            holder.teacherName= (TextView) convertView.findViewById(R.id.uv_tv_teachername);
            holder.title= (TextView) convertView.findViewById(R.id.uv_tv_title);
            holder.swipeView= (MySwipeView) convertView.findViewById(R.id.uv_swipeview);
            holder.uvDelete= (TextView) convertView.findViewById(R.id.uv_view_delete);
            holder.coverPic = (ImageView) convertView.findViewById(R.id.uv_coverpic);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        holder.swipeView.setListView(listView);
        holder.swipeView.setTag(holder.swipeView.getId(), position);

        holder.teacherName.setText(uvData.get(position).getMemname());
        holder.playTimes.setText(uvData.get(position).getViews()+"");
        holder.title.setText(uvData.get(position).getTitle());
        holder.commend.setText(uvData.get(position).getLikes()+"");

        if (uvData.get(position).getAvatar()!=null&&!uvData.get(position).getAvatar().equals("")) {
            String avatar = uvData.get(position).getAvatar();
            ImageLoader.getInstance().displayImage(avatar, holder.avator);
        }
        //防止图片错位
        int id = uvData.get(position).getId();
        holder.coverPic.setTag(id);
        String path = uvData.get(position).getCoverpic();
        if (path != null && !path.equals("")) {
            if (holder.coverPic.getTag() != null && holder.coverPic.getTag().equals(id)) {
                ImageLoader.getInstance().displayImage(path,holder.coverPic);
                Log.e("test", "视频封面  " + path);
            }
        }

        //左滑删除
        holder.uvDelete.setTag(""+position);
        onTouchVideoDel.onTouchDel(holder.uvDelete,position);
        return convertView;
    }
    private class ViewHolder{
        CircleImageView avator;
        TextView title,teacherName,commend,playTimes,uvDelete;
        MySwipeView swipeView;
        ImageView coverPic;
    }

    public interface onTouchVideoDel{
        void onTouchDel(TextView tvDel,int position);
    }
}
